/**
 * ajax请求
 * @param {Object} Vue Vue的实例
 * @param {Object} options 请求配置公用
 * @return {Function} 返回当前@ajax
 */
import axios from 'axios'
import router from '@/router'
import {
		setToken
	} from '@/utils/auth'
import { newWorkTextConfig, newWorkProConfig, SUCCESS_REQUEST_STATUS } from './config.js'
export default {
	install(Vue, options) {
		// 默认参数
		const DEFAULT_CONFIG = {
			// 接口名称
			api: '',
			// 默认POST请求
			type: 'POST',
			// 请求参数
			params: {},
		}
		Vue.prototype.$ajax = ((option) => {
			// 拼接参数
			if(!option) console.log('请求参数不能为空')
			let resData = Object.assign({}, DEFAULT_CONFIG, option)
			// 判断当前的请求方式如果是get合并请求
			let url = resData.type == 'GET' ? `${newWorkProConfig.b_server_url}${option.api}/${mergeUrl(resData)}` : `${newWorkProConfig.b_server_url}${option.api}`
			// 判断当前是否是普通的Content-Type
			let contentType = resData.contentType ? 'application/json' : resData.params instanceof FormData ? 'multipart/form-data' : 'application/x-www-form-urlencoded'
			console.log(url)
			return new Promise((resolve, reject) => {
				axios({
					url: url,
					data: resData.params,
					method: resData.type,
					transformRequest: [(data) => {
						// 如果当前是get请求
						let ret = ''
						if(resData.type == 'GET') {
							for(let it in resData.params) {
								ret += encodeURIComponent(it) + '=' + encodeURIComponent(data[it]) + '&'
							}
						} else {
							// 处理请求参数
							ret = resData.params instanceof FormData ? resData.params : resData.contentType ? JSON.stringify(resData.params) : handleParams(resData.params)
						}
						return ret
					}],
					headers: {
						'Content-Type': contentType
					}
				}).then(res => {
					console.log(res)
					if(res.data.status === SUCCESS_REQUEST_STATUS || res.status === SUCCESS_REQUEST_STATUS) {
						if(res.data.status === 403) {
							reject({
								status: res.data.status,
								message: res.data.message
							})
							router.push('/login')
							setToken('')
							return
						}
						resolve({
							data: res.data.data ? res.data.data : res.data,
							status: res.data.status ? res.data.status : res.status
						})
						return
					} else {

						reject({
							status: res.data.status,
							message: res.data.message
						})
					}
				}).catch(err => {
					reject({
						message: '服务器报错'
					})
					router.push('/login')
					setToken('')

				})

			})
		})
	}
}

/**
 * @author yang 
 *	@param param 请求的参数
 * @return 返回拼接后的参url参数
 */
const mergeUrl = (options) => {
	let url = ''
	for(let k in options.params) {
		let value = options.params[k] !== undefined ? options.params[k] : ''
		url += '&' + k + '=' + encodeURIComponent(value)
	}
	return url ? url.substring(1) : ''
}

/**
 * @author yang 
 * @param param 请求的参数
 * @return 返回拼接后的参数
 */
const handleParams = (param) => {
	let params = []
	let result = null
	let keys = Object.keys(param)
	for(let i = 0, len = keys.length; i < len; i++) {
		// 去除空参数
		if(param[keys[i]] !== undefined && param[keys[i]] !== null) {
			param[keys[i]] = encodeURIComponent(param[keys[i]])
			params.push(keys[i] + '=' + param[keys[i]])
		}
	}
	result = params.join('&')
	return result
}

const handleJsonParams = (param) => {
	let keys = Object.keys(param)
	for(let i = 0, len = keys.length; i < len; i++) {
		// 去除空参数
		if(param[keys[i]] !== undefined && param[keys[i]] !== null) {
			param[keys[i]] = param[keys[i]]
			console.log(param[keys[i]])
		}
	}
}